package com.hllxd.hism.star.basic.service.dao.http;

import com.alibaba.fastjson.JSON;
import com.hllxd.hism.star.basic.service.config.properties.SmartApiProperties;
import com.hllxd.hism.star.basic.service.exception.BusinessException;
import com.hllxd.hism.star.basic.service.model.smartdq.QueryData;
import com.hllxd.hism.star.basic.service.model.smartdq.SmartResult;
import com.github.pagehelper.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * 查询数据
 * @author lixiyong
 * @date 2019-01-02 10:11
 **/
@Slf4j
@Component("smartApiMapper")
public class SmartApiMapperImpl implements SmartApiMapper {

    @Autowired
    private SmartApiProperties properties;

    private static String URL = "http://localhost:9081";
    private static String ACCESS_TOKEN = "p0r1o2d3u4c5t6O7u8t9Store0123456";

    @PostConstruct
    private void initialize() {
        URL = properties.host;
        ACCESS_TOKEN = properties.accessToken;
    }

    @Override public Object handle(String queryCmd, Map<String, String> queryConfig, String queryCode, Long queryId, String owner) {
        if(StringUtil.isEmpty(queryCmd)) {
            log.warn("params is invalid.");
            throw new BusinessException("302","params is invalid.");
        }
        try {
            QueryData param = new QueryData();
            param.business = queryCode;
            param.scene = queryId;
            param.cmd = queryCmd;
            param.config = queryConfig;
            param.user = owner;
            param.execUser = "zpbigdata_wr";
            param.token = ACCESS_TOKEN;

            String requestData = JSON.toJSONString(param);
            byte[] postData = requestData.getBytes(StandardCharsets.UTF_8);
            URL obj = new URL(URL+"/smartdq/api/smartdq-engine-service/v1/query/query.do");
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type", "application/json");
            con.setRequestProperty("Accept", "application/json");
            con.setDoOutput(true);
            try (DataOutputStream wr = new DataOutputStream(con.getOutputStream())) {
                wr.write(postData);
            }
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuilder response = new StringBuilder();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            SmartResult obj2 = JSON.parseObject(response.toString(),SmartResult.class);
            System.out.println("choices: " + JSON.toJSONString(obj2.queryResult.data.get(0)));

            return obj2;
        }catch (Exception e) {
            log.warn("Exception: {}.",e.toString());
            return null;
        }
    }



    public static void main(String[] args) throws IOException {
        
    }
}
